package leetcode_core.leetcode_6;

import org.junit.Test;

import java.util.*;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年02月02日 13:30
 */
public class GroupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        //1. key->value
        //key?是以字母出现的频数作为键,aabbcc=>2a2b2c
        Map<String,List<String>> map = new HashMap<>();
        for(int i = 0;i<strs.length;i++){
            char[] strArray = strs[i].toCharArray();
            int[] count = new int[26];
            for(int j = 0;j<strArray.length;j++){
                count[strArray[j] - 'a'] ++;
            }
            StringBuilder sb = new StringBuilder();
            for(char c = 'a';c<='z';c++){
                int cnt = count[c-'a'];
                if(cnt == 0){
                    continue;
                }
                sb.append(cnt).append(c);
            }
            String key = sb.toString();
            map.computeIfAbsent(key,ops-> new ArrayList<>());
            map.get(key).add(strs[i]);
        }

        return new ArrayList<>(map.values());
    }

    @Test
    public void test(){
        System.out.println(groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }
}
